package com.example.demo;



import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;

import com.baomidou.mybatisplus.generator.config.DataSourceConfig;

import com.baomidou.mybatisplus.generator.config.OutputFile;

import org.springframework.beans.factory.annotation.Value;



import java.sql.SQLException;

import java.util.Collections;

import java.util.Scanner;


public class MyGenerator {
    /**
     * 数据源配置
     */
    private static final DataSourceConfig.Builder DATA_SOURCE_CONFIG = new DataSourceConfig
            .Builder("jdbc:mysql://localhost:3306/device?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai", "root", "root");
    /**

     * 执行 run

     */

    public static void main(String[] args) throws SQLException {

        String projectPath = System.getProperty("user.dir");

        String tableName = scanner("表名");

        FastAutoGenerator.create(DATA_SOURCE_CONFIG)

                // 全局配置

                .globalConfig(builder -> {

                    builder.author("zhengyingya") // 设置作者

                            .fileOverride() // 覆盖已生成文件

                            .outputDir(projectPath + "/src/main/java"); // 指定输出目录

                })

                // 包配置

                .packageConfig((scanner, builder) -> {

                    builder.parent("com.example.demo.modules")

                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, projectPath + "/src/main/resources/mapper"));

                })

                // 策略配置

                .strategyConfig(builder ->

                        builder.addInclude(tableName)

                                .entityBuilder()

                                .enableLombok()

                                .enableTableFieldAnnotation()     // 开启生成实体时生成字段注解

                                .controllerBuilder()

                                .enableRestStyle()                // 开启生成@RestController 控制器

                                .mapperBuilder()

                                .enableMapperAnnotation()

                )

                /*

                    模板引擎配置，默认 Velocity 可选模板引擎 Beetl 或 Freemarker

                   .templateEngine(new BeetlTemplateEngine())

                   .templateEngine(new FreemarkerTemplateEngine())

                 */

                .execute();

    }



    /**

     * 读取控制台内容信息

     */

    private static String scanner(String tip) {

        Scanner scanner = new Scanner(System.in);

        System.out.println(("请输入" + tip + "："));

        if (scanner.hasNext()) {

            String next = scanner.next();

            if (StringUtils.isNotBlank(next)) {

                return next;

            }

        }

        throw new MybatisPlusException("请输入正确的" + tip + "！");

    }

}